1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5
6 public class TileManager : MonoBehaviour {
7
8 public class Tile
9 {
10 public int x { get; set; }
11 public int y { get; set; }
12 public bool occupied {get; set;}
13 public int adjacentCount {get; set;}
14 public bool isIntersection {get; set;}
15
16 public Tile left,right,up,down;
17
18 public Tile(int x_in, int y_in)
19 {
20 x = x_in; y = y_in;
21 occupied = false;
22 left = right = up = down = null;
23 }
24
25
26 };
27
28 public List<Tile> tiles = new List<Tile>();
29
30 // Use this for initialization
31 void Start ()
32 {
33 ReadTiles();
34
35 }
36
37 // Update is called once per frame
38 void Update ()
39 {
40 //DrawNeighbors();
41
42 }
43
44 //-----------------------------------------------------------------------
45 // hardcoded tile data: 1 = free tile, 0 = wall
46 void ReadTiles()
47 {
48 // hardwired data instead of reading from file (not feasible on web player)
49 string data = @"0000000000000000000000000000
50 0111111111111001111111111110
51 0100001000001001000001000010
52 0100001000001111000001000010
53 0100001000001001000001000010
54 0111111111111001111111111110
55 0100001001000000001001000010
56 0100001001000000001001000010
57 0111111001111001111001111110
58 0001001000001001000001001000
59 0001001000001001000001001000
60 0111001111111111111111001110
61 0100001001000000001001000010
62 0100001001000000001001000010
63 0111111001000000001001111110
64 0100001001000000001001000010
65 0100001001000000001001000010
66 0111001001111111111001001110
67 0001001001000000001001001000
68 0001001001000000001001001000
69 0111111111111111111111111110
70 0100001000001001000001000010
71 0100001000001001000001000010
72 0111001111111001111111001110
73 0001001001000000001001001000
74 0001001001000000001001001000
75 0111111001111001111001111110
76 0100001000001001000001000010
77 0100001000001001000001000010
78 0111111111111111111111111110
79 0000000000000000000000000000";
80
81 int X = 1, Y = 31;
82 using (StringReader reader = new StringReader(data))
83 {
84 string line;
85 while ((line = reader.ReadLine()) != null)
86 {
87
88 X = 1; // for every line
89 for (int i = 0; i < line.Length; ++i)
90 {
91 Tile newTile = new Tile(X, Y);
92
93 // if the tile we read is a valid tile (movable)
94 if (line[i] == '1')
95 {
96 // check for left-right neighbor
97 if (i != 0 && line[i - 1] == '1')
98 {
99 // assign each tile to the corresponding side of other tile
100 newTile.left = tiles[tiles.Count - 1];
101 tiles[tiles.Count - 1].right = newTile;
102
103 // adjust adjcent tile counts of each tile
104 newTile.adjacentCount++;
105 tiles[tiles.Count - 1].adjacentCount++;
106 }
107 }
108
109 // if the current tile is not movable
110 else newTile.occupied = true;
111
112 // check for up-down neighbor, starting from second row (Y<30)
113 int upNeighbor = tiles.Count - line.Length; // up neighbor index
114 if (Y < 30 && !newTile.occupied && !tiles[upNeighbor].occupied)
115 {
116 tiles[upNeighbor].down = newTile;
117 newTile.up = tiles[upNeighbor];
118
119 // adjust adjcent tile counts of each tile
120 newTile.adjacentCount++;
121 tiles[upNeighbor].adjacentCount++;
122 }
123
124 tiles.Add(newTile);
125 X++;
126 }
127
128 Y--;
129 }
130 }
131
132 // after reading all tiles, determine the intersection tiles
133 foreach (Tile tile in tiles)
134 {
135 if (tile.adjacentCount > 2)
136 tile.isIntersection = true;
137 }
138
139 }
140
141 //-----------------------------------------------------------------------
142 // Draw lines between neighbor tiles (debug)
143 void DrawNeighbors()
144 {
145 foreach(Tile tile in tiles)
146 {
147 Vector3 pos = new Vector3(tile.x, tile.y, 0);
148 Vector3 up = new Vector3(tile.x+0.1f, tile.y+1, 0);
149 Vector3 down = new Vector3(tile.x-0.1f, tile.y-1, 0);
150 Vector3 left = new Vector3(tile.x-1, tile.y+0.1f, 0);
151 Vector3 right = new Vector3(tile.x+1, tile.y-0.1f, 0);
152
153 if(tile.up != null) Debug.DrawLine(pos, up);
154 if(tile.down != null) Debug.DrawLine(pos, down);
155 if(tile.left != null) Debug.DrawLine(pos, left);
156 if(tile.right != null) Debug.DrawLine(pos, right);
157 }
158
159 }
160
161
162 //----------------------------------------------------------------------
163 // returns the index in the tiles list of a given tile's coordinates
164 public int Index(int X, int Y)
165 {
166 // if the requsted index is in bounds
167 //Debug.Log ("Index called for X: " + X + ", Y: " + Y);
168 if(X>=1 && X<=28 && Y<=31 && Y>=1)
169 return (31-Y)*28 + X-1;
170
171 // else, if the requested index is out of bounds
172 // return closest in-bounds tile's index
173 if(X<1) X = 1;
174 if(X>28) X = 28;
175 if(Y<1) Y = 1;
176 if(Y>31) Y = 31;
177
178 return (31-Y)*28 + X-1;
179 }
180
181 public int Index(Tile tile)
182 {
183 return (31-tile.y)*28 + tile.x-1;
184 }
185
186 //----------------------------------------------------------------------
187 // returns the distance between two tiles
188 public float distance(Tile tile1, Tile tile2)
189 {
190 return Mathf.Sqrt( Mathf.Pow(tile1.x - tile2.x, 2) + Mathf.Pow(tile1.y - tile2.y, 2));
191 }
192 }
Use this for initialization
Update is called once per frame
DrawNeighbors();
-----------------------------------------------------------------------
hardcoded tile data: 1 = free tile, 0 = wall
hardwired data instead of reading from file (not feasible on web player)
X = 1; for every line
if the tile we read is a valid tile (movable)
check for left-right neighbor
assign each tile to the corresponding side of other tile
adjust adjcent tile counts of each tile
if the current tile is not movable
check for up-down neighbor, starting from second row (Y<30)
int upNeighbor = tiles.Count - line.Length; up neighbor index
adjust adjcent tile counts of each tile
after reading all tiles, determine the intersection tiles
-----------------------------------------------------------------------
Draw lines between neighbor tiles (debug)
----------------------------------------------------------------------
returns the index in the tiles list of a given tile's coordinates
if the requsted index is in bounds
Debug.Log ("Index called for X: " + X + ", Y: " + Y);
else, if the requested index is out of bounds
return closest in-bounds tile's index
----------------------------------------------------------------------
returns the distance between two tiles